/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.gui;

import javax.swing.JDialog;

/**
 * A Dialog where text can be input and returned.
 * 
 * @author PeZiii
 */
public class TextInputDlg extends javax.swing.JDialog {
    
    private String text;
    private boolean ok;
    
    /**
     * Returns the text
     * 
     * @return text 
     */
    public String getText() {
        return text;
    }
    
    /**
     * Returns dialog-control-variable for the parent component
     * 
     * @return dialog-control-variable
     */
    public boolean isOk() {
        return ok;
    }

    /**
     * Create new form TextInputDlg
     * Set location and title
     * Put delivered text into textfield
     * 
     * @param title The title of the dialog
     * @param text The text which should be shown in the textfield
     */
    public TextInputDlg(JDialog parent, boolean modal, String title, String text) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        setTitle(title);
        tfInput.setText(text);
        tfInput.requestFocusInWindow();
    }
    
    /**
     * Create new form TextInputDlg
     * Set location and title
     * 
     * @param title The title of the dialog
     */
    public TextInputDlg(JDialog parent, boolean modal, String title) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        setTitle(title);
        tfInput.requestFocusInWindow();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfInput = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        tfInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                onKeyPressed(evt);
            }
        });

        jLabel2.setText("Name:");

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onOk(evt);
            }
        });

        jButton2.setText("Abbrechen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onCancel(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addGap(26, 26, 26)
                        .addComponent(tfInput, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 1, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Ok event 
     * Sets dialog-control-variable true 
     * The entered text can now be retrieved by the parent component
     * 
     * @param evt
     */
    private void onOk(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onOk
        ok = true;
        text = tfInput.getText();
        dispose();
    }//GEN-LAST:event_onOk
    
    /**
     * Cancel event 
     * Sets dialog-control-variable false
     * 
     * @param evt
     */
    private void onCancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onCancel
        ok = false;
        dispose();
    }//GEN-LAST:event_onCancel
    
    /**
     * KeyReleased event for the textfield (which is always focused)
     * If Key is Enter, call ok event 
     * If Key is Esc, call cancel event
     * 
     * @param evt
     */
    private void onKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_onKeyPressed
        if (evt.getKeyCode() == 10) {
            onOk(null);
        } else if (evt.getKeyCode() == 27) {
            onCancel(null);
        }
    }//GEN-LAST:event_onKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField tfInput;
    // End of variables declaration//GEN-END:variables
}
